package com.simple.datax.flow.service.impl;

import java.util.Date;

import org.jfree.util.Log;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.cache.TaskCacheBean;
import com.simple.datax.cache.model.TaskState;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.constants.TaskConstants;
import com.simple.datax.flow.dao.ibatis.FlowDao;
import com.simple.datax.flow.dao.ibatis.FlowInstanceDao;
import com.simple.datax.flow.dao.ibatis.model.Flow;
import com.simple.datax.flow.dao.ibatis.model.FlowInstance;
import com.simple.datax.flow.service.TaskManager;

/** 
 * @Title: FlowInstanceManagerImpl.java 
 * @Package com.simple.datax.flow.service.impl 
 * @Description: TODO(报关任务管理器) 
 * @author tonney 
 * @date 2012-12-31 上午11:17:16 
 * @version V1.0 
 */
public class TaskManagerImpl implements TaskManager {
	public Logger log = LoggerFactory.getLogger(this.getClass());

	private FlowInstanceDao flowInstanceDao;
    
	private FlowDao flowDao;
	
	private TaskCacheBean taskCache;
	
	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}

	public void setTaskCache(TaskCacheBean taskCache) {
		this.taskCache = taskCache;
	}

	public void setFlowInstanceDao(FlowInstanceDao flowInstanceDao) {
		this.flowInstanceDao = flowInstanceDao;
	}

	@Override
	public void saveFlowInstance(FlowInstance flowInstance) {
		flowInstanceDao.saveFlowInstance(flowInstance);
	}

	@Override
	public int validateTaskid(String flowid,String taskid,String userid) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
		FlowInstance flowInstance = null;
		Flow flow = null;
		boolean isrestart = false;
		boolean isnewtask = true;
		try{
			flow = flowDao.getFlowById(flowid);
			if(flow==null){
				Log.warn("TaskManagerImpl::validateTaskid  invalid flowid");
				result = ProcessResultConstants.STATUS_FLOW_NULL;
				return result;
			}
			String status = taskCache.getTaskStatus(taskid);
			if(status==null||status.equals("")){  //缓存中没有此任务信息,则判断是否缓存重启过
				isrestart = taskCache.hasRestart();
				if(isrestart){  //缓存重启过,去数据库校验活动号
					flowInstance = flowInstanceDao.getFlowInstance(taskid);
					if(flowInstance!=null){   //任务在数据库已存在
						isnewtask = false;
						int dbstatus = flowInstance.getStatus();
						if(dbstatus!=TaskConstants.STATUS_SUCCESS){  //任务已完成
							//缓存重启过，并且在任务在数据库中有记录，则添加任务至缓存
							taskCache.setTaskStatus(taskid, ""+TaskConstants.STATUS_DEALING);
							taskCache.setTaskFlow(taskid, flowInstance.getFlowId());
						}
					}
				}
			}else{
				isnewtask = false;
			}
			
			if(status == null||status.equals("")){
				if(isnewtask){  //新创建任务，验证任务ID是否合法
					if(taskid.indexOf(userid,0)!=0){//任务ID不合法则返回
						result = ProcessResultConstants.STATUS_TASK_IDILLEAGE;
						return result;
					}
				}
				//缓存为空并且缓存没有重启过||缓存重启过但是从数据库未查到任务记录,则说明是新任务,能走到这步说明是新任务
				flowInstance = new FlowInstance();
				flowInstance.setTaskId(taskid);
				flowInstance.setBusinessType(flow.getBusinessType());
				flowInstance.setCreatedDate(new Date());
				flowInstance.setFlowId(flowid);
				flowInstance.setOriginateCop(flow.getOriginateCop());
//				flowInstance.setPriority(""+flow.getPriority());
				flowInstance.setNotes(flow.getNotes());
				flowInstance.setFlowName(flow.getFlowName());
				flowInstance.setStatus(TaskConstants.STATUS_DEALING);
				flowInstanceDao.saveFlowInstance(flowInstance);
				taskCache.setTaskStatus(taskid, ""+TaskConstants.STATUS_DEALING);
				taskCache.setTaskFlow(taskid, flowid);
			}else{
				if(status.equals(TaskConstants.STATUS_SUCCESS)){
					result = ProcessResultConstants.STATUS_TASK_OVER;
				}
			}
		}catch(Exception e){
			result =  ProcessResultConstants.STATUS_PROC_EXCEPTION;
			log.error("TaskManagerImpl::validateTaskid error=>"+e.toString());
		}
		return result;
	}

//	public TaskState getTaskStateByTaskid(String taskId) {
//		TaskState taskState = null;
//		taskState = taskCache.getTaskState(taskId);
//		if(taskState==null){
//			if(taskCache.hasRestart()){   //需要从数据库查询任务状态
//				
//			}
//		}
//		return taskState;
//	}

	@Override
	public void updateFlowInstanceStatus(String taskid,int status) {
		flowInstanceDao.updateFlowInstanceStatus(taskid,status);
	}
	
	public String getFlowidByTaskid(String taskid) {
		String flowid = null;
		if(taskid!=null){
			flowid = taskCache.getTaskFlow(taskid);
			if(flowid == null){   //缓存中没有则从数据库提取
				FlowInstance flowInstance = flowInstanceDao.getFlowInstance(taskid);
				if(flowInstance!=null){
					flowid = flowInstance.getFlowId();
					taskCache.setTaskFlow(taskid, flowid);
				}
			}
		}
		return flowid;
	}

}
